<template> 
  <div class="app-container adminUser">
    <el-card class="filter-container" shadow="never">
        <div>
          <i class="el-icon-search"></i>
          <span>筛选搜索</span>
          <el-button
            style="float: right"
            @click="searchAdminList()"
            type="primary"
            size="small">
            查询结果
          </el-button>
        </div>
        <div style="margin-top: 15px">
          <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
            <el-form-item label="输入搜索：">
              <el-input style="width: 203px" v-model="listQuery.name" placeholder="用户名称/关键字"></el-input>
            </el-form-item>
          </el-form>
        </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        class="btn-add"
        @click="addAdmin()"
        size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="adminTable"
                :data="list"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                v-loading="listLoading"
                border>

        <!-- <el-table-column type="selection" width="60" align="center"></el-table-column> -->
        <!-- <el-table-column label="编号" width="180" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column> -->
        <el-table-column label="用户账号" width="180" align="center">
          <template slot-scope="scope">{{scope.row.username}}</template>
        </el-table-column>
        <el-table-column label="注册时间" width="180" align="center">
          <template slot-scope="scope">{{scope.row.createTime | formatTime}}</template>
        </el-table-column>
        <el-table-column label="用户昵称" align="center">
          <template slot-scope="scope">{{scope.row.nickName}}</template>
        </el-table-column>

        <el-table-column label="邮件" width="180" align="center">
          <template slot-scope="scope">{{scope.row.email}}</template>
        </el-table-column>

        <!-- <el-table-column label="是否禁用" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleShowStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column> -->

        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
               type="primary"
              @click="handleChoose(scope.$index, scope.row)">角色分配
            </el-button>
            <el-button
              size="mini"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div> -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>


    <el-dialog
      title="权限分配"
      :visible.sync="roleShow" width="30%">
      <el-form
        ref="adminFrom"
        :model="admin"
        label-width="100px"
      >
        <el-form-item
          label="角色："
          prop="roleIds"
        >
          <el-checkbox-group v-model="allRoles1">
            <el-checkbox
              v-for="(item,index) in allRoles"
              :key="index"
              :checked="item.status == 1"
              :label="item.id"
            >{{item.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="roleShow = false">取 消</el-button>
        <el-button type="primary" @click="handleuserMessageConfirm">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>
<script>
import {
  createAdmin,
  getAdmin,
  updateAdmin,
  updateAdminRole,
  userRoles,
  checkPermission
} from "@/api/sys/admin";

  import {formatDate} from '@/utils/date';
  import {fetchList, updateShowStatus, updateFactoryStatus, deleteAdmin} from '@/api/sys/admin'

  export default {
    name: 'adminList',
    data() {
      return {
        allRoles: null,
        userRoles: null,
        roleIds: null,
        admin:{
          roleIds: [],
        },


        operates: [
          {
            label: "显示",
            value: "showAdmin"
          },
          {
            label: "隐藏",
            value: "hideAdmin"
          }
        ],
        adminId: '',
        operateType: null,
        listQuery: {
          name: null,
          pageNum: 1,
          pageSize: 10
        },
        list: null,
        total: null,
        listLoading: true,
        multipleSelection: [],
        roleShow: false,
      }
    },

    computed: {
      //选中的服务保证
      allRoles1: {
        get() {
          let list = [];
          if (
            this.roleIds === undefined ||
            this.roleIds == null ||
            this.roleIds === ""
          )
            return list;
          let ids = this.roleIds.split(",");
          for (let i = 0; i < ids.length; i++) {
            list.push(Number(ids[i]));
          }
          return list;
        },
        set(newValue) {
          let serviceIds = "";
          if (newValue != null && newValue.length > 0) {
            for (let i = 0; i < newValue.length; i++) {
              serviceIds += newValue[i] + ",";
            }
            if (serviceIds.endsWith(",")) {
              serviceIds = serviceIds.substr(0, serviceIds.length - 1);
            }
            this.roleIds = serviceIds;
          } else {
            this.roleIds = null;
          }
        }
      }
    },


    created() {
      this.getList();
    },
    filters:{
      formatTime(time) {
        if(time==null||time===''){
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
      formatStatus(status){
        for(let i=0;i<defaultStatusOptions.length;i++){
          if(status===defaultStatusOptions[i].value){
            return defaultStatusOptions[i].label;
          }
        }
      },
      formatReturnAmount(row){
        return row.productRealPrice*row.productCount;
      }
    },
    methods: {
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
          this.totalPage = response.data.pages;
          this.pageSize = response.data.size;
        });
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleUpdate(index, row) {
        this.$router.push({path: '/sys/updateAdmin', query: {id: row.id}})
      },

      // 编辑角色
      handleChoose(index, row) {
        this.adminId = row.id
         this.roleIds = ''
        console.log('row',row.id)
        this.roleShow = true
        checkPermission(row.id).then(res => {
          if (res.code == 200) {
            // console.log(res);
            this.allRoles = res.data;
          }
        });

        // getAdmin(this.$route.query.id).then(response => {
        //   this.admin = response.data;
        // });
        userRoles(row.id).then(response => {
          let roleIds = []
          console.log('response',response)
          if (response.data.length == 0) {
            this.roleIds = ''
          } else {
            console.log('response',response.data)
            response.data.forEach(item => {
              console.log('resItem',item)
              if (item) {

                roleIds.push(item.id)
              }
            })
            this.roleIds = roleIds.join(',')
          }
        });
      },
      handleuserMessageConfirm() {
          // POST /admin/role/update
          console.log('this.roleIds',this.roleIds)
          let obj = {}
          obj.roleIds = this.roleIds
          obj.adminId = this.adminId
          // return
          updateAdminRole(obj).then(
            response => {
              if (response.code == 200) {
                this.$refs['adminFrom'].resetFields();
                this.$message({
                  message: "修改成功",
                  type: "success",
                  duration: 1000
                });
                this.roleIds = ''
                this.admin.roleIds = []
                this.roleShow = false
                // this.$router.back();
              } else {
                this.$message({
                  message: response.msg,
                  type: "error",
                  duration: 1000
                });
              }
            }
          );
      },



      handleDelete(index, row) {
        this.$confirm('是否要删除该记录', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let praArr = []
          praArr.push(row.id)
          deleteAdmin(praArr).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        });
      },
      getProductList(index, row) {
        console.log(index, row);
      },
      getProductCommentList(index, row) {
        console.log(index, row);
      },
      handleFactoryStatusChange(index, row) {
        var data = new URLSearchParams();
        data.append("ids", row.id);
        data.append("factoryStatus", row.factoryStatus);
        updateFactoryStatus(data).then(response => {
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        }).catch(error => {
          if (row.factoryStatus === 0) {
            row.factoryStatus = 1;
          } else {
            row.factoryStatus = 0;
          }
        });
      },
      handleShowStatusChange(index, row) {
        let data = new URLSearchParams();
        ;
        data.append("ids", row.id);
        data.append("showStatus", row.showStatus);
        updateShowStatus(data).then(response => {
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        }).catch(error => {
          if (row.showStatus === 0) {
            row.showStatus = 1;
          } else {
            row.showStatus = 0;
          }
        });
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      searchAdminList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleBatchOperate() {
        console.log(this.multipleSelection);
        if (this.multipleSelection < 1) {
          this.$message({
            message: '请选择一条记录',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let showStatus = 0;
        if (this.operateType === 'showAdmin') {
          showStatus = 1;
        } else if (this.operateType === 'hideAdmin') {
          showStatus = 0;
        } else {
          this.$message({
            message: '请选择批量操作类型',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        let data = new URLSearchParams();
        data.append("ids", ids);
        data.append("showStatus", showStatus);
        updateShowStatus(data).then(response => {
          this.getList();
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        });
      },
      addAdmin() {
        this.$router.push({path: '/sys/addAdmin'})
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .adminUser /deep/ .el-checkbox+.el-checkbox {
      margin-left: 0px;
      margin-right: 30px;
  }

</style>


